package com.zlll.winner.service.media;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.media.MediaHost;
import com.zlll.winner.business.model.sysUser.SysUser;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.mapper.media.MediaHostMapper;
import com.zlll.winner.business.service.media.IMediaHostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  主持人服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-10-11
 */
@Service
public class MediaHostServiceImpl extends ServiceImpl<MediaHostMapper, MediaHost> implements IMediaHostService {

    @Autowired
    private MediaHostMapper mediaHostMapper;

    /**
     * 分页查询主持人信息
     * @param params
     * @return
     */
    @Override
    public IPage<Map<String, Object>> fetchHostListPage(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        String name = params.getString("name");
        IPage<Map<String, Object>> page = new Page<>(pageNo, pageSize);
        List<Map<String, Object>> list = mediaHostMapper.fetchHostListPage(name, page);
        page.setRecords(list);
        return page;
    }

    /**
     * 保存主持人
     * @param mediaHost
     * @param user
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public boolean saveHost(MediaHost mediaHost, SysUser user) {
        mediaHost.setCreateTime(System.currentTimeMillis());
        mediaHost.setCreateUser(user.getName());
        mediaHost.setIsDel(CommonEnum.YesOrNo.NO.val());
        //新增广告记录并返回自增id
        Integer result = mediaHostMapper.insert(mediaHost);
        //新增记录为零
        if (result == 0) {
            return true;
        }
        return false;
    }

    /**
     * 编辑主持人
     * @param mediaHost
     * @param user
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void editHost(MediaHost mediaHost, SysUser user) {
        mediaHost.setUpdateUser(user.getName());
        mediaHost.setUpdateTime(System.currentTimeMillis());
        mediaHostMapper.updateById(mediaHost);
    }

    /**
     * 删除主持人信息
     * @param id
     * @param name
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int deleteHostById(Long id, String name) {
        MediaHost mediaHost = new MediaHost();
        //删除主持人
        mediaHost.setId(id);
        mediaHost.setUpdateUser(name);
        mediaHost.setUpdateTime(System.currentTimeMillis());
        mediaHost.setIsDel(CommonEnum.DelEnum.DEL.val());
        return mediaHostMapper.updateById(mediaHost);
    }

    /**
     * 获取主持人信息
     * @param menuId
     * @return
     */
    @Override
    public List<MediaHost> getMediaHostByMenuId(Long menuId) {
        QueryWrapper<MediaHost> query = new QueryWrapper<MediaHost>();
        query.eq("is_del",CommonEnum.DelEnum.UNDEL.val());
        query.eq("menu_id", menuId);
        return mediaHostMapper.selectList(query);
    }

}
